<html xmlns:x="http://www.zkoss.org/2005/zul" xmlns:zk="http://www.zkoss.org/2005/zk" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>zhtml test</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAFsbk3MCxLHMqk4JK-UAkuRRmepwflZi_YL9iCBtMwnYXKjaXLhQnPeL1mUQ0VGjzyZRrw1PlG75_ZA" type="text/javascript"/>
</head>
<body>

  <zscript>
    import at.ac.tuwien.tubuddy.*;
    import org.json.JSONObject;
    import org.json.JSONArray;
    DBUserManager dbusermanager = new DBUserManager();
    DBBuddyManager dbbuddymanager = new DBBuddyManager();
    User user = null;
    JSONArray buddylist = null;
  </zscript>
  
  <zscript>
    import org.zkoss.zul.*;
  </zscript>
  
  <x:window id="login_register_reset_win" width="300px" mode="overlapped" position="center" border="none" visible="true" onRightClick=""> 
    <x:tabbox id="login_register_reset_tabbox" width="256px">
      <x:tabs>
        <x:tab label="login" action="onclick:login_submit.disabled=false;action.hide(#{login_error_div});#{login_username}.value=&quot;&quot;;#{login_password}.value=&quot;&quot;;"/>
        <x:tab label="register" action="onclick:register_validate_submit.disabled=false;register_submit.disabled=false;action.hide(#{register_error_div});action.hide(#{register_validate_error_div});#{register_username}.value=&quot;&quot;;#{register_password1}.value=&quot;&quot;;#{register_password}.value=&quot;&quot;;#{register_email}.value=&quot;&quot;;#{register_validate_hashvalue}.value=&quot;&quot;;"/>
        <x:tab label="reset password" action="onclick:reset_submit.disabled=false;action.hide(#{reset_error_div});#{reset_email}.value=&quot;&quot;;"/>
      </x:tabs>
      <x:tabpanels>
        <x:tabpanel>
          <x:div id="login_error_div" align="center" visible="false">
            <x:label id="login_error_msg" style="color:red;font-weight:bolder;" value=" "/>
          </x:div>
          <x:grid>
            <x:rows>
              <x:row>
                nickname: <x:textbox width="150px" id="login_username" name="login_username" constraint="/[a-zA-Z0-9_]*/"/>
              </x:row>
              <x:row>
                password: <x:textbox width="150px" id="login_password" name="login_password" type="password" constraint="/[a-zA-Z0-9]*/"/>
              </x:row>
            </x:rows>
          </x:grid>
          <x:textbox id="login_password_encrypt" value=" " visible="false"/>
          <x:button label="login" id="login_submit" action="onclick:encrypt_password(#{login_password},#{login_password_encrypt});">
            <attribute name="onClick">
              login_submit.setDisabled(true);
              login_error_div.setVisible(false);
              if(login_username.getValue().trim().equals("") || login_password.getValue().trim().equals("")) {
                login_error_msg.setValue("fill in!");
                login_error_div.setVisible(true);
                login_submit.setDisabled(false);
              }
              else if(!dbusermanager.isRegistered(login_username.getValue().trim())) {
                login_error_msg.setValue("no such user!");
                login_error_div.setVisible(true);
                login_submit.setDisabled(false);
              }
              else if(!dbusermanager.isUser(login_username.getValue().trim())) {
                login_error_msg.setValue("commit registration before login!");
                login_error_div.setVisible(true);
                login_submit.setDisabled(false);
              }
              else if(dbusermanager.validateUser(login_username.getValue().trim(),login_password_encrypt.getValue().trim())) {
                login_register_reset_win.setVisible(false);
                user = dbusermanager.getUserByUsername(login_username.getValue().trim());
                loadBuddyList();
                Path.getComponent(&quot;/buddylist_win/buddylist_caption&quot;).setLabel("TUbuddy: " + login_username.getValue().trim());
                buddylist_win.setVisible(true);
                Path.getComponent(&quot;/buddylist_win/buddylist_tree_online&quot;).setOpen(true);
                Path.getComponent(&quot;/buddylist_win/buddylist_tree_offline&quot;).setOpen(true);
                Path.getComponent(&quot;/buddylist_win/buddylist_tree_other&quot;).setOpen(true);
                gmap_win.setVisible(true);
              }
              else {
                login_error_msg.setValue("wrong password!");
                login_error_div.setVisible(true);
                login_submit.setDisabled(false);
              }
            </attribute>
          </x:button>
          <script src="md5.js"/>
          <script type="text/javascript">
            function encrypt_password(pswdbox,pswdbox_encrypt) {
              pswdbox_encrypt.value = hex_md5(pswdbox.value);
              if (document.createEvent) {
                var evt = document.createEvent('HTMLEvents');
                evt.initEvent('blur', false, false);
                pswdbox_encrypt.dispatchEvent(evt);
              }
              else if (document.createEventObject) {
                pswdbox_encrypt.fireEvent('onblur');
              }
            }
          </script>
        </x:tabpanel>
        <x:tabpanel>
          <x:div id="register_error_div" align="center" visible="false">
            <x:label id="register_error_msg" style="color:red;font-weight:bolder;" value=" "/>
          </x:div>
          <x:grid>
            <x:rows>
              <x:row>
                nickname: <x:textbox width="150px" id="register_username" constraint="/[a-zA-Z0-9_]*/"/>
              </x:row>
              <x:row>
                password: <x:textbox width="150px" id="register_password1" type="password" constraint="/[a-zA-Z0-9]*/"/>
              </x:row>
              <x:row>
                confirm: <x:textbox width="150px" id="register_password2" type="password" constraint="/[a-zA-Z0-9]*/"/>
              </x:row>
              <x:row>
                email: <x:textbox width="150px" id="register_email" constraint="/^[a-zA-Z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/: Please enter a valid e-mail address"/>
              </x:row>
            </x:rows>
          </x:grid>
          <x:textbox id="register_password1_encrypt" value=" " visible="false"/>
          <x:textbox id="register_password2_encrypt" value=" " visible="false"/>
          <x:button label="register" id="register_submit" action="onclick:encrypt_password(#{register_password1},#{register_password1_encrypt});encrypt_password(#{register_password2},#{register_password2_encrypt});">
            <attribute name="onClick">
              register_submit.setDisabled(true);
              register_error_div.setVisible(false);
              if(register_username.getValue().trim().equals("") || register_password1.getValue().trim().equals("") || register_password2.getValue().trim().equals("") || register_email.getValue().trim().equals("")) {
                register_error_msg.setValue("fill in!");
                register_error_div.setVisible(true);
                register_submit.setDisabled(false);
              }
              else if(!register_password1_encrypt.getValue().trim().equals(register_password2_encrypt.getValue().trim())) {
                register_error_msg.setValue("passwords do not match!");
                register_error_div.setVisible(true);
                register_submit.setDisabled(false);
              }
              else if(dbusermanager.isUser(register_username.getValue().trim()) || dbusermanager.isRegistered(register_username.getValue().trim())) {
                register_error_msg.setValue("username already taken!");
                register_error_div.setVisible(true);
                register_submit.setDisabled(false);
              }
              else if(dbusermanager.isEmailUsed(register_email.getValue().trim())) {
                register_error_msg.setValue("email already used!");
                register_error_div.setVisible(true);
                register_submit.setDisabled(false);
              }
              //add new registered user to the register db and send him an email
              else if(dbusermanager.addRegistered(register_email.getValue().trim(), register_password2_encrypt.getValue().trim(), register_username.getValue().trim())) {
                register_error_msg.setValue("successfully registered!");
                register_error_div.setVisible(true);
              }
              else {
                register_error_msg.setValue("problem encountered!");
                register_error_div.setVisible(true);
                //register_submit.setDisabled(false);
              }
            </attribute>
          </x:button>
          <x:div id="register_validate_error_div" align="center" visible="false">
            <x:label id="register_validate_error_msg" style="color:red;font-weight:bolder;" value=" "/>
          </x:div>
          <x:grid>
            <x:rows>
              <x:row>
                validation code: <x:textbox id="register_validate_hashvalue" constraint="/[a-zA-Z0-9]*/"/>
              </x:row>
            </x:rows>
          </x:grid>
          <x:button label="validate" id="register_validate_submit">
            <attribute name="onClick">
              String hashvalue = register_validate_hashvalue.getValue().trim();
              register_validate_error_div.setVisible(false);
              register_validate_submit.setDisabled(true);
              if (hashvalue.equals("")) {
                register_validate_error_msg.setValue("fill in!");
                register_validate_error_div.setVisible(true);
                register_validate_submit.setDisabled(false);
              }
              else if (dbusermanager.validateRegistered(hashvalue)) {
                register_validate_error_msg.setValue("successfully validated!");
                register_validate_error_div.setVisible(true);
              }
              else {
                register_validate_error_msg.setValue("wrong validation code!");
                register_validate_error_div.setVisible(true);
                register_validate_hashvalue.setValue("");
                register_validate_submit.setDisabled(false);
              }
            </attribute>
          </x:button>
        </x:tabpanel>
        <x:tabpanel>
          <x:div id="reset_error_div" align="center" visible="false">
            <x:label id="reset_error_msg" style="color:red;font-weight:bolder;" value=" "/>
          </x:div>
          <x:grid>
            <x:rows>
              <x:row>
                email: <x:textbox width="150px" id="reset_email" constraint="/^[a-zA-Z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/: Please enter a valid e-mail address"/>
              </x:row>
            </x:rows>
          </x:grid>
          <x:button label="reset" id="reset_submit">
            <attribute name="onClick">
              reset_error_div.setVisible(false);
              reset_submit.setDisabled(true);
              if((!reset_email.getValue().trim().equals(""))) {
                reset_error_msg.setValue("fill in!");
                reset_error_div.setVisible(true);
                reset_submit.setDisabled(false);
              }
              else if(dbusermanager.resetEMail(reset_email.getValue().trim())) {
                login_error_msg.setValue("password reseted!");
                login_error_div.setVisible(true);
              }
              else {
                reset_error_msg.setValue("no such email address!");
                reset_error_div.setVisible(true);
                reset_submit.setDisabled(false);
              }
            </attribute>
          </x:button>
        </x:tabpanel>
      </x:tabpanels>
    </x:tabbox>
  </x:window>

  <zscript>
    void compareBuddyList() {
      JSONArray buddylist_new = dbbuddymanager.getBuddyList();
      JSONArray buddylist_old = buddylist;

      for (int i = 0; i &lt; buddylist.length(); i++) {
        String buddy_nickname = buddylist.getJSONObject(i).get("nickname");
        String buddy_user_to_buddy = buddylist.getJSONObject(i).get("user_to_buddy");
        String buddy_buddy_to_user = buddylist.getJSONObject(i).get("buddy_to_user");
      }
    }
    void loadBuddyList() {
      buddylist = (JSONArray)dbbuddymanager.getBuddyList(user.nickname);
      for (int i = 0; i &lt; buddylist.length(); i++) {
        String buddy_nickname = buddylist.getJSONObject(i).get("nickname");
        String buddy_user_to_buddy = buddylist.getJSONObject(i).get("user_to_buddy");
        String buddy_buddy_to_user = buddylist.getJSONObject(i).get("buddy_to_user");
        int buddy_is_online = buddylist.getJSONObject(i).getInt("is_online");
        int buddy_visibility = buddylist.getJSONObject(i).getInt("visibility");
                
        Treeitem buddy_treeitem = new Treeitem(buddy_nickname);
        buddy_treeitem.setImage("./offline.png");
        buddy_treeitem.setParent(Path.getComponent(&quot;/buddylist_win/buddylist_tree_offline_children&quot;));
        buddy_treeitem.setContext("buddy_context");        
        continue;
        
        if (buddy_user_to_buddy.equals("") || buddy_user_to_buddy.equals("remove") || buddy_user_to_buddy.equals("remove_invisible") || buddy_user_to_buddy.equals("blocked")) {
          //don't show the buddy in the buddylist at all
          continue;
        }
        if (buddy_user_to_buddy.equals("deny_blocked")) {
          Window dlg = new Window();
          dlg.setBorder("normal");
          dlg.setTitle("denied!");
          Label lbl = new Label(buddy_nickname + " denied your request..<br/><br/>");
          lbl.setparent(dlg);
          Button btn = new Button("ok");
          btn.setParent(dlg);
          btn.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event) throws UiException {
              dbbuddymanager.changeUserToBuddy(user.nickname, buddy_nickname, "blocked");
              event.getReference().getParent().detach();
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          dlg.doModal();
          //don't show the buddy in the buddylist at all
          continue;
        }
        if (buddy_user_to_buddy.equals("denied")) {
          Window dlg = new Window();
          dlg.setBorder("normal");
          dlg.setTitle("denied!");
          Label lbl = new Label(buddy_nickname + " denied your request..<br/><br/>");
          lbl.setparent(dlg);
          Button btn = new Button("ok");
          btn.setParent(dlg);
          btn.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event) throws UiException {
              dbbuddymanager.changeUserToBuddy(user.nickname, buddy_nickname, "");
              event.getReference().getParent().detach();
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          dlg.doModal();
          //don't show the buddy in the buddylist at all
          continue;
        }
        if(buddy_buddy_to_user.equals("remove") || buddy_buddy_to_user.equals("denied")) {
          //doesnt matter for me..
        }
        if(buddy_buddy_to_user.equals("add")) {
          Window dlg = new Window();
          dlg.setBorder("normal");
          dlg.setTitle("add request..");
          Label lbl = new Label(buddy_nickname + " wants to add you..<br/><br/>");
          lbl.setparent(dlg);
          Button btn_add = new Button("authorize(add)");
          btn_add.setParent(dlg);
          btn_add.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event) throws UiException {
              dbbuddymanager.addBuddy(user.nickname, buddy_nickname);
              event.getReference().getParent().detach();
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          Button btn_deny = new Button("deny");
          btn_deny.setParent(dlg);
          btn_deny.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event) throws UiException {
              dbbuddymanager.changeUserToBuddy(buddy_nickname, user.nickname, "denied");
              event.getReference().getParent().detach();
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          Button btn_block = new Button("block");
          btn_block.setParent(dlg);
          btn_block.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event) throws UiException {
              dbbuddymanager.changeUserToBuddy(buddy_nickname, user.nickname, "deny_blocked");
              event.getReference().getParent().detach();
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          dlg.doModal();
        }
        
        if(buddy_buddy_to_user.equals("blocked") || buddy_buddy_to_user.equals("deny_blocked")) {
          buddy_treeitem.setLabel(buddy_treeitem.getLabel() + " (blocked)");
          addContext("unblock", buddy_nickname, buddy_context);
          addContext("remove", buddy_nickname, buddy_context);
          addContext("rename", buddy_nickname, buddy_context);
          buddy_treeitem.setContext("buddylist_treeitem_context_" + buddy_nickname);
        }
        if(buddy_user_to_buddy.equals("invisible")) {
          buddy_treeitem.setLabel(buddy_treeitem.getLabel() + " (blind)");
          addContext("visible", buddy_nickname, buddy_context);
          addContext("remove", buddy_nickname, buddy_context);
          addContext("rename", buddy_nickname, buddy_context);
          buddy_treeitem.setContext("buddylist_treeitem_context_" + buddy_nickname);
        }
        
        if (buddy_user_to_buddy.equals("add")) {
          buddy_treeitem.setLabel(buddy_treeitem.getLabel() + " (not authorized yet)");
          buddy_treeitem.setImage("./other.png");
          buddy_treeitem.setParent(Path.getComponent(&quot;/buddylist_win/buddylist_tree_other_children&quot;));
          addContext("remove", buddy_nickname, buddy_context);
          addContext("rename", buddy_nickname, buddy_context);
          buddy_treeitem.setContext("buddylist_treeitem_context_" + buddy_nickname);
          continue;
        }
        if(buddy_buddy_to_user.equals("remove_invisible") || buddy_buddy_to_user.equals("invisible")) {
          buddy_treeitem.setImage("./offline.png");
          buddy_treeitem.setParent(Path.getComponent(&quot;/buddylist_win/buddylist_tree_offline_children&quot;));
          addContext("remove", buddy_nickname, buddy_context);
          addContext("rename", buddy_nickname, buddy_context);
          buddy_treeitem.setContext("buddylist_treeitem_context_" + buddy_nickname);
          continue;
        }
        
        if (buddy_visibility==0 || buddy_is_online==0) {
          buddy_treeitem.setImage("./offline.png");
          buddy_treeitem.setParent(Path.getComponent(&quot;/buddylist_win/buddylist_tree_offline_children&quot;));
        }
        else if (buddy_is_online==1 &amp;&amp; buddy_visibility==1) {
          buddy_treeitem.setImage("./online.png");
          buddy_treeitem.setParent(Path.getComponent(&quot;/buddylist_win/buddylist_tree_online_children&quot;));
        }
        
        /**
        buddy_treeitem.setImage("./offline.png");
        buddy_treeitem.setParent(Path.getComponent(&quot;/buddylist_win/buddylist_tree_offline_children&quot;));
        addContext("", buddy_nickname, buddy_context);
        buddy_treeitem.setContext("buddylist_treeitem_context_" + buddy_nickname);
        continue;
        **/
      }
    }
    
    void addContext(String action, String buddy_username, Menupopup treeitem_menupopup) {
      Menuitem mi = new Menuitem(action + " " + buddy_username);
      treeitem_menupopup.appendChild(mi);
      /**
      Menuitem mi = new Menuitem(action + " " + buddy_username);
      if (action.equals("remove")) {
        mi.addEventListener("onClick", new EventListener(){
          public void onEvent(Event event2) throws UiException {
            event2.getReference().detach();
          }
          public boolean isAsap() {
            return true;
          }
        }
        );
      }
      else if (action.equals("rename")) {
        mi.addEventListener("onClick", new EventListener(){
          public void onEvent(Event event2) throws UiException {
            //TODO: modal dialog: rename buddy
          }
          public boolean isAsap() {
            return true;
          }
        }
        );
      }
      else if (action.equals("unblock")) {
        mi.addEventListener("onClick", new EventListener(){
          public void onEvent(Event event2) throws UiException {
            //TODO: label treeitem -(blocked)
            //TODO: change in db
          }
          public boolean isAsap() {
            return true;
          }
        }
        );
      }
      else if (action.equals("visible")) {
        mi.addEventListener("onClick", new EventListener(){
          public void onEvent(Event event2) throws UiException {
            //TODO: label treeitem -(blind)
            //TODO: change in db
          }
          public boolean isAsap() {
            return true;
          }
        }
        );
      }
      
      mi.setParent(treeitem_menupopup);
      **/
    }
  </zscript>

  <x:window id="buddylist_win" width="220px" mode="overlapped" position="right,top" border="normal" sizable="true" visible="false">
    <x:caption id="buddylist_caption" image="" label="TUbuddy">
      <x:toolbarbutton id="buddylist_minimize" label="_"/>
      <x:toolbarbutton id="buddylist_maximize" label="O"/>
      <x:toolbarbutton id="buddylist_close" label="X"/>
    </x:caption>
    <x:toolbar id="buddylist_toolbar" width="98%">
      <x:toolbarbutton id="buddylist_add" label=".add buddy." onClick="buddylist_add_dlg.doModal();"/>
      <x:toolbarbutton id="buddylist_locate" label=".locate me." onClick="if(!gmap_win.isVisible()) gmap_win.setVisible(true); Path.getComponent(&quot;/gmap_win/gmap_mymap&quot;).panTo(48.4410,16.1490);"/>
      <x:toolbarbutton id="buddylist_invis" label=".invisible me." onClick="if(self.getLabel().equals(&quot;.invisible me.&quot;)) {self.setLabel(&quot; .visible me. &quot;);} else {self.setLabel(&quot;.invisible me.&quot;);}"/>
      <x:toolbarbutton id="buddylist_settings" label=".settings." onClick="buddylist_settings_dlg.doModal();"/>
      <x:toolbarbutton id="buddylist_help" label=".help." onClick="buddylist_help_dlg.doModal();" visible="false"/>
    </x:toolbar>
    <x:tree id="buddylist_tree">
      <x:treechildren>
        <x:treeitem id="buddylist_tree_online" label="Online" open="false">
          <x:treechildren id="buddylist_tree_online_children">
<!--            <x:treeitem label="mama" value="v_mama" image="./online.png" context="ppup1"/> -->
          </x:treechildren>
        </x:treeitem>
        <x:treeitem id="buddylist_tree_offline" label="Offline" open="false">
          <x:treechildren id="buddylist_tree_offline_children">
<!--            <x:treeitem label="buddy1" value="v_buddy1" image="./offline.png" context="ppup2"/> -->
          </x:treechildren>
        </x:treeitem>
        <x:treeitem id="buddylist_tree_other" label="Other" open="false">
          <x:treechildren id="buddylist_tree_other_children">
<!--            <x:treeitem label="buddy1" value="v_buddy1" image="./other.png" context="ppup2"/> -->
          </x:treechildren>
        </x:treeitem>
      </x:treechildren>
    </x:tree>

    <x:menupopup id="foo_menupopup">
      <x:menuitem label="remove"/>
      <x:menuitem label="rename"/>
    </x:menupopup>

    <x:menupopup id="buddy_context">
      <attribute name="onOpen">
        if(event.isOpen()) {
          if (!self.getChildren().isEmpty()) {
            self.removeChild(self.getChildren().get(0));
            self.removeChild(self.getChildren().get(0));
            self.removeChild(self.getChildren().get(0));
          }
          Component who = event.getReference().getParent();
          
          Menuitem label_remove = new Menuitem("remove "+who.getLabel());
          label_remove.setParent(self);
          label_remove.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event2) throws UiException {
              //dbbuddymanager.removeBuddy(who.getLabel());
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          
          Menuitem label_rename = new Menuitem("rename "+who.getLabel());
          label_rename.setParent(self);
          label_rename.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event2) throws UiException {
              //
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          
          Menuitem label_offline = new Menuitem("offline "+who.getLabel());
          label_offline.setParent(self);
          label_offline.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event2) throws UiException {
              who.getParent().removeChild(who);
              who.setImage("./offline.png");
              who.setContext(ppup2.getId());
              who.setParent(buddylist_tree_offline_children);
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
        }
      </attribute>
    </x:menupopup>
<!--
    <x:menupopup id="ppup2">
      <attribute name="onOpen">
        if(event.isOpen()) {
          if (!self.getChildren().isEmpty()) {
            self.removeChild(self.getChildren().get(0));
            self.removeChild(self.getChildren().get(0));
          }
          Component who = event.getReference().getParent();
          
          Menuitem label_remove = new Menuitem("remove "+who.getLabel());
          label_remove.setParent(self);
          label_remove.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event2) throws UiException {
              who.getParent().removeChild(who);
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
          
          Menuitem label_online = new Menuitem("online "+who.getLabel());
          label_online.setParent(self);
          label_online.addEventListener("onClick", new EventListener(){
            public void onEvent(Event event2) throws UiException {
              who.getParent().removeChild(who);
              who.setImage("./online.png");
              who.setContext(ppup1.getId());
              who.setParent(buddylist_tree_online_children);
            }
            public boolean isAsap() {
              return true;
            }
          }
          );
        }
      </attribute>
    </x:menupopup>
-->
  </x:window>

  <x:window id="buddylist_add_dlg" width="220px" title="add buddy" mode="overlapped" border="normal" position="center" visible="false">
    <x:label style="font-size:xx-small;" value="Please enter the name of the user you would like to add to your buddylist."/>
    <x:div id="buddylist_add_error_div" align="center" visible="false">
      <x:label id="buddylist_add_error_msg" value=" "/>
    </x:div>
    Username: <x:textbox id="buddylist_add_dlg_buddy" constraint="/[a-zA-Z0-9_]*/: Please use only alphanumeric characters"/><br/><br/>
    <x:button label="add" id="buddylist_add_submit">
      <attribute name="onClick">
        buddylist_add_error_div.setVisible(false);
        buddylist_add_submit.setDisabled(true);
        String buddy_username = buddylist_add_dlg_buddy.getValue().trim();
        Buddy buddy = null;
        if(buddy_username.equals("")) {
        buddylist_add_error_msg.setStyle("color:red;font_weight:bolder;");
          buddylist_add_error_msg.setValue("fill in!");
          buddylist_add_error_div.setVisible(true);
          buddylist_add_submit.setDisabled(false);
        }
        else if(!dbusermanager.isUser(buddy_username)) {
        buddylist_add_error_msg.setStyle("color:red;font_weight:bolder;");
          buddylist_add_error_msg.setValue("no such user!");
          buddylist_add_error_div.setVisible(true);
          buddylist_add_submit.setDisabled(false);
        }
        else if(!dbbuddymanager.addBuddy(user.nickname,buddy_username)) {
          buddylist_add_error_msg.setStyle("color:red;font_weight:bolder;");
          buddylist_add_error_msg.setValue("you cannot add this user!");
          buddylist_add_error_div.setVisible(true);
          buddylist_add_submit.setDisabled(false);
        }
        else {
          buddylist_add_error_msg.setStyle("color:green;font_weight:bolder;");
          buddylist_add_error_msg.setValue("buddy successfully added!");
          buddylist_add_error_div.setVisible(true);
          buddylist_add_submit.setDisabled(false);
        }
        /**
        Treeitem buddy_foo = new Treeitem(buddylist_add_dlg_buddy.value);
        buddy_foo.setValue(&quot;v_&quot;+buddylist_add_dlg_buddy.value);
        buddy_foo.setImage(&quot;./offline.png&quot;);
        buddy_foo.setContext(Path.getComponent(&quot;/buddylist_win/ppup1&quot;).getId());
        Path.getComponent(&quot;/buddylist_win/buddylist_tree_offline_children&quot;).appendChild(buddy_foo);
        **/
        buddylist_add_dlg.doOverlapped();
        buddylist_add_dlg.setVisible(false);
      </attribute>
    </x:button>
    <x:button label="back" onClick="buddylist_add_dlg.doOverlapped();buddylist_add_dlg.setVisible(false);"/>
  </x:window>
  
  <x:window id="buddylist_settings_dlg" width="330px" title="settings" mode="overlapped" border="normal" position="center" visible="false">
    <x:button label="OK" onClick="buddylist_settings_dlg.doOverlapped();buddylist_settings_dlg.setVisible(false);"/>
    my profile..<br/>blocked list, ignore list, ...<br/>
    <x:div id="changepwd_error_div" align="center" visible="false">
      <x:label id="changepwd_error_msg" style="color:red;font-weight:bolder;" value=" "/>
    </x:div>
    <x:grid>
      <x:rows>
        <x:row>
          current password: <x:textbox id="changepwd_pwd_old" type="password" constraint="/[a-zA-Z0-9]*/"/>
        </x:row>
        <x:row>
          new password: <x:textbox id="changepwd_pwd_new1" type="password" constraint="/[a-zA-Z0-9]*/"/>
        </x:row>
        <x:row>
          confirm password: <x:textbox id="changepwd_pwd_new2" type="password" constraint="/[a-zA-Z0-9]*/"/>
        </x:row>
      </x:rows>
    </x:grid>
    <x:textbox id="changepwd_pwd_old_encrypt" value=" " visible="false"/>
    <x:textbox id="changepwd_pwd_new1_encrypt" value=" " visible="false"/>
    <x:textbox id="changepwd_pwd_new2_encrypt" value=" " visible="false"/>
    <x:button id="changepwd_submit" label="change password" action="onclick:encrypt_password(#{changepwd_pwd_old},#{changepwd_pwd_old_encrypt});encrypt_password(#{changepwd_pwd_new1},#{changepwd_pwd_new1_encrypt});encrypt_password(#{changepwd_pwd_new2},#{changepwd_pwd_new2_encrypt});">
      <attribute name="onClick">
        changepwd_submit.setDisabled(true);
        changepwd_error_div.setVisible(false);
        String pwd_old = changepwd_pwd_old_encrypt.getValue().trim();
        String pwd_new1 = changepwd_pwd_new1_encrypt.getValue().trim();
        String pwd_new2 = changepwd_pwd_new2_encrypt.getValue().trim();
        if (changepwd_pwd_old.getValue().trim().equals("") || changepwd_pwd_new1.getValue().trim().equals("") || changepwd_pwd_new2.getValue().trim().equals("")) {
          changepwd_error_msg.setValue("fill in!");
          changepwd_error_div.setVisible(true);
          changepwd_submit.setDisabled(false);
        }
        else if (!changepwd_pwd_old_encrypt.getValue().trim().equals(user.password)) {
          changepwd_error_msg.setValue("wrong old password!");
          changepwd_error_div.setVisible(true);
          changepwd_submit.setDisabled(false);
        }
        else if (!pwd_new1.equals(pwd_new2)) {
          changepwd_error_msg.setValue("new passwords do not match!");
          changepwd_error_div.setVisible(true);
          changepwd_submit.setDisabled(false);
        }
        else {
          //change the password in user db
          dbusermanager.changePassword(user.nickname, pwd_new2);
          changepwd_error_msg.setValue("password changed!");
          changepwd_error_div.setVisible(true);
        }
      </attribute>
    </x:button>
  </x:window>
  
  <x:window id="buddylist_help_dlg" width="220px" title="help" mode="overlapped" border="normal" position="center" visible="false">
    help..<br/><br/>
    <x:button label="OK" onClick="buddylist_help_dlg.doOverlapped();buddylist_help_dlg.setVisible(false);"/>
  </x:window>

  <zscript>
    import org.zkforge.gmaps.Gmaps;
  </zscript>

  <x:window id="gmap_win" mode="overlapped" border="normal" position="center" width="402px" height="425px" visible="false">
    <x:caption id="gmap_caption" image="" label="Google Map">
      <x:toolbarbutton id="gmap_close" label="X" onClick="gmap_win.setVisible(false);"/>
    </x:caption>
   	<x:gmaps id="gmap_mymap" lat="37.4410" lng="-122.1490" width="390px" height="390px" showSmallCtrl="true"/>
  </x:window>

</body>
</html>
